package com.giscloud.amp.service.auth.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.giscloud.amp.dao.AmpRoleMapper;
import com.giscloud.amp.entity.AmpRole;
import com.giscloud.amp.entity.AmpRoleExample;
import com.giscloud.amp.entity.dto.AmpRoleDTO;
import com.giscloud.amp.entity.vo.AmpRoleVO;
import com.giscloud.amp.service.auth.AmpRoleService;
import com.giscloud.datasource.page.PageInfo;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Author:gxd
 * @Description:
 * @Date: 11:04 2018/2/24
 * @Modified By:
 */
@Service("ampRoleService")
public class AmpRoleServiceImpl implements AmpRoleService {


    private final static Logger log = LoggerFactory.getLogger(AmpRoleServiceImpl.class);

    @Autowired
    private AmpRoleMapper ampRoleMapper;

    @Override
     public PageInfo getAmpRoleList(AmpRoleDTO ampRoleDTO) {
        log.debug("#getAmpRoleList parameter ={}", JSON.toJSONString(ampRoleDTO));
        PageHelper.startPage(ampRoleDTO.getPage(), ampRoleDTO.getRows());
        AmpRoleExample ampRoleExample = new AmpRoleExample();
        AmpRoleExample.Criteria criteria = ampRoleExample.createCriteria();
        if(StringUtils.isNotBlank(ampRoleDTO.getRoleName())){
            criteria.andRoleNameLike("%"+ampRoleDTO.getRoleName()+"%");
        }
        if(ampRoleDTO.getSystemId() !=null && ampRoleDTO.getSystemId()!=-1){
            criteria.andSystemIdEqualTo(ampRoleDTO.getSystemId());
        }
        criteria.andStatusCdEqualTo((short)1);
        List<AmpRoleVO> ampRoles = ampRoleMapper.selectByExample(ampRoleExample);
        return PageInfo.of(ampRoles);
    }


    @Override
    public AmpRoleVO getAmpRoleByCondition(AmpRoleDTO ampRoleDTO) {
        log.debug("#getAmpRoleByCondition parameter ={}", JSON.toJSONString(ampRoleDTO));
        AmpRoleExample ampRoleExample = new AmpRoleExample();
        AmpRoleExample.Criteria criteria = ampRoleExample.createCriteria();
        if(StringUtils.isNotBlank(ampRoleDTO.getRoleName())){
            criteria.andRoleNameEqualTo(ampRoleDTO.getRoleName());
        }
        if(ampRoleDTO.getSystemId() != null){
            criteria.andSystemIdEqualTo(ampRoleDTO.getSystemId());
        }
        if(ampRoleDTO.getRoleId() != null){
            criteria.andRoleIdEqualTo(ampRoleDTO.getRoleId());
        }
        criteria.andStatusCdEqualTo((short)1);
        List<AmpRoleVO> ampRoles = ampRoleMapper.selectByExample(ampRoleExample);
        if(!CollectionUtil.isEmpty(ampRoles) && ampRoles.size() == 1){
            return ampRoles.get(0);
        }else{
            return  null;
        }
    }
    @Override
    @Transactional(propagation= Propagation.REQUIRED, rollbackFor=Exception.class)
    public int insert(AmpRoleDTO ampRoleDTO) {
        AmpRole ampRole = new AmpRole();
        BeanUtils.copyProperties(ampRoleDTO,ampRole);
        ampRole.setCreatedDt(new Date());
        ampRole.setVersion(new Date());
        int i =  ampRoleMapper.insert(ampRole);
        return i;
    }
    @Override
    @Transactional(propagation= Propagation.REQUIRED, rollbackFor=Exception.class)
    public int update(AmpRoleDTO ampRoleDTO) {
        AmpRoleExample ampRoleExample = new AmpRoleExample();
        AmpRoleExample.Criteria criteria = ampRoleExample.createCriteria();
        if(ampRoleDTO.getRoleId() != null){
            criteria.andRoleIdEqualTo(ampRoleDTO.getRoleId());
        }
        AmpRole ampRole = new AmpRole();
        BeanUtils.copyProperties(ampRoleDTO,ampRole);
        ampRole.setVersion(new Date());
        int i = ampRoleMapper.updateByExample(ampRole,ampRoleExample);
        return i;
    }
    @Override
    @Transactional(propagation= Propagation.REQUIRED, rollbackFor=Exception.class)
    public int delete(Long roleId) {
        AmpRoleExample ampRoleExample = new AmpRoleExample();
        AmpRoleExample.Criteria criteria = ampRoleExample.createCriteria();
        criteria.andRoleIdEqualTo(roleId);
        return ampRoleMapper.deleteByExample(ampRoleExample);
    }
}
